package com.tb.ClassSchedule;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guich on 2016/8/20.
 */
public class SectionSelector extends AlertDialog.Builder implements NumberPicker.Formatter,NumberPicker.OnValueChangeListener{

    private NumberPicker weekDayPicker;
    private NumberPicker     startSectionPicker;
    private NumberPicker     endSectionPicker;
    private String[] data = { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
    private String[] data2 = { "第1节" , "第2节" , "第3节" , "第4节" , "第5节" , "第6节" , "第7节" , "第8节" , "第9节" , "第10节" , "第11节"};

    public SectionSelector(Context context) {
        super(context);
        setTitle("选择上课节数");
        View contentView = LayoutInflater.from(context).inflate(R.layout.select_section,null);
        setView(contentView);

        weekDayPicker = (NumberPicker) contentView.findViewById(R.id.week_day);
        startSectionPicker = (NumberPicker) contentView.findViewById(R.id.start_section);
        endSectionPicker = (NumberPicker) contentView.findViewById(R.id.end_section);

        weekDayPicker.setDisplayedValues(data);
        weekDayPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        weekDayPicker.setMinValue(1);
        weekDayPicker.setMaxValue(data.length);
        startSectionPicker.setDisplayedValues(data2);
        startSectionPicker.setOnValueChangedListener(this);
        startSectionPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        startSectionPicker.setMinValue(1);
        startSectionPicker.setMaxValue(11);
        endSectionPicker.setDisplayedValues(data2);
        endSectionPicker.setOnValueChangedListener(this);
        startSectionPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        endSectionPicker.setMinValue(1);
        endSectionPicker.setMaxValue(11);


        setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                AddClassActivity.weekday = weekDayPicker.getValue();
                AddClassActivity.startSection = startSectionPicker.getValue();
                AddClassActivity.endSection = endSectionPicker.getValue();
                Log.d(getClass().getName(),"" + weekDayPicker.getValue() + startSectionPicker.getValue() + endSectionPicker.getValue());

                String str = "";
                str += (data[weekDayPicker.getValue() - 1]);
                str += (" 第" + startSectionPicker.getValue());
                if(startSectionPicker.getValue() == endSectionPicker.getValue()){
                    str += ("节");
                }else{
                    str += (" ~ " + endSectionPicker.getValue() + "节");
                }
                AddClassActivity.selectSection.setText(str);

            }
        });
        setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });

    }

    public SectionSelector(Context context,int week,int start,int end) {
        this(context);
        weekDayPicker.setValue(week);
        startSectionPicker.setValue(start);
        endSectionPicker.setValue(end);
    }


    @Override
    public String format(int i) {
        return "第" + i + "节";
    }

    @Override
    public void onValueChange(NumberPicker numberPicker, int i, int i1) {
        if(endSectionPicker.getValue() < startSectionPicker.getValue()){
            endSectionPicker.setValue(startSectionPicker.getValue());
        }
    }
}
